home *** CD-ROM | disk | FTP | other *** search
- Path: osf1.gmu.edu!rraffer1
- From: rraffer1@osf1.gmu.edu (Ryan M Rafferty)
- Newsgroups: comp.lang.c++
- Subject: Bletcherous kludge!
- Date: 29 Mar 1996 23:17:31 GMT
- Organization: George Mason University, Fairfax, Virginia, USA
- Message-ID: <4jhr2b$9ou@portal.gmu.edu>
- NNTP-Posting-Host: osf1.gmu.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=US-ASCII
- Content-Transfer-Encoding: 7bit
- X-Newsreader: TIN [version 1.2 PL2]
-
- I am trying to get the contents of a custom String class to be converted
- into numeric data, ie float. The String class consists of :
-
- Buffer: a null-terminated array of char
-
- and several methods for common string stuff, including overloaded << and >>
- operators. The code listed below is the only method I've been able to come up
- with that works, but it is really an awful hack that outputs the contents of
- the String instance to a file and then reads it back in to do the conversion.
-
- I tried creating an instance of iostream, inputting to it from the String
- instance, and then outputting from it into a float variable, but it just
- didn't work. Any suggestions on how to do this the right way would be
- greatly appreciated!
-
- Ryan Rafferty
-
- #include "String.h"
-
- float String::Number() {
-
- float i = 0;
-
- // this conversion method is, in its current state, a HORRIBLE and
- // AWFUL kludge to convert the contents of a String instance to a
- // numeric type. It doesn't do any error checking, but what's worse,
- // it USES A TEMPORARY FILE !!!! I'm so mad at myself! But I can't
- // seem to be able to bend the will of the iostreams class to do what
- // I want!
-
- ofstream fout("./__temp");
- fout << Buffer;
- fout.close();
- ifstream fin("./__temp");
- fin >> i;
- fin.close();
-
- // YECH! A feeble attempt to hide my shame...
- // execl("/bin/rm", "rm", "./__temp", (char *) 0);
-
- return i;
- };
-